package com.zxs.live_model.widget

import android.animation.AnimatorSet
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.MotionEvent.*
import androidx.appcompat.widget.AppCompatImageView
import com.asa.common.widgets.ButtonAnim

/**
 *  Author   yuanfangyan
 *  Create   2020_05_11 19:35
 *  Describe
 **/
class AnimClickImageView : AppCompatImageView {
    private var mDownAnim: AnimatorSet? = null
    private var mUpAnim: AnimatorSet? = null

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
            context,
            attrs,
            defStyleAttr
    ) {
        init()
    }

    private fun init() {
        mDownAnim = ButtonAnim.loadDownAnim(this)
        mUpAnim = ButtonAnim.loadUpAnim(this)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        if (event?.action == ACTION_DOWN) {
            mDownAnim?.start()
        } else if (event?.action == ACTION_UP || event?.action == ACTION_CANCEL) {
            mDownAnim?.cancel()
            mUpAnim?.start()
        }
        return super.onTouchEvent(event)
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        mDownAnim!!.cancel()
        mUpAnim!!.cancel()
    }
}